﻿using System;
using System.IO.IsolatedStorage;
using nRoute.Services;
using nRoute.Components.Composition;

namespace nRoute.Samples.ExtendedMVVM.Services
{
    [MapService(typeof(ILocalStoreService), InitializationMode = InitializationMode.OnDemand, Lifetime=InstanceLifetime.Singleton)]
    public class LocalStoreService
        : ILocalStoreService
    {
        public void Save(string key, string value)
        {
            if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key");

            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
            {
                IsolatedStorageSettings.ApplicationSettings[key] = value;
            }
            else
            {
                IsolatedStorageSettings.ApplicationSettings.Add(key, value);
            }
        }

        public string Load(string key)
        {
            if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key");

            if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
            {
                return Convert.ToString(IsolatedStorageSettings.ApplicationSettings[key]);
            }
            else
            {
                return null;
            }
        }
    }
}
